package org.social.module;

import org.bean.StoreInfo;
import org.bean.StoreInfo.ServerLimit;
import org.constant.ActionEnum;
import org.social.db.bean.CommodityBuyRecord;
import org.social.db.cache.CommodityBuyRecordCache;
import org.utils.RemoteUtil;

import com.sojoys.artifact.exception.AbstractLogicModelException;

/**
 * @author : DengYing
 * @CreateDate : 2017年12月6日 下午3:46:08
 * @Description ：Please describe this document
 */
public class CommodityManager {
	private CommodityManager() {
	}

	private static CommodityManager ME = new CommodityManager();

	public static CommodityManager me() {
		return ME;
	}
	
	public void addCommodityBuyRecord(int commodityId, int num) throws AbstractLogicModelException {
		CommodityBuyRecord commodityBuyRecord = CommodityBuyRecordCache.me().getByCommodityId(commodityId);
		if (commodityBuyRecord == null) {
			commodityBuyRecord = new CommodityBuyRecord(0, commodityId, num, num);
			commodityBuyRecord.insert();
		} else {
			int nowBuyCount = commodityBuyRecord.getNowBuyCount() + num;
			int totalBuyCount = commodityBuyRecord.getTotalBuyCount() + num;

			commodityBuyRecord.setNowBuyCount(nowBuyCount);
			commodityBuyRecord.setTotalBuyCount(totalBuyCount);
			commodityBuyRecord.update();
		}

		StoreInfo stortInfo = new StoreInfo(ActionEnum.UPDATE);
		stortInfo.getServerLimits().add(new ServerLimit(commodityId,commodityBuyRecord.getNowBuyCount(), commodityBuyRecord.getTotalBuyCount()));
		RemoteUtil.gameTopic().noticeStore(stortInfo);
	}
}
